/****************************************************************************
 * Copyright (c) 2010 Torkild U. Resheim
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Torkild U. Resheim  - initial API and implementation
 *****************************************************************************/
package no.resheim.buildmonitor.hudson;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.MessageFormat;

import no.resheim.buildmonitor.core.BuildmonitorCore;
import no.resheim.buildmonitor.core.data.Server;

import org.eclipse.ecf.core.ContainerConnectException;
import org.eclipse.ecf.core.ContainerCreateException;
import org.eclipse.ecf.core.ContainerFactory;
import org.eclipse.ecf.core.IContainer;
import org.eclipse.ecf.discovery.IDiscoveryLocator;
import org.eclipse.ecf.discovery.IServiceEvent;
import org.eclipse.ecf.discovery.IServiceInfo;
import org.eclipse.ecf.discovery.IServiceListener;
import org.eclipse.ecf.discovery.IServiceProperties;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 * 
 * @author Torkild Ulvøy Resheim
 * @since 1.0
 */
public class HudsonPlugin extends AbstractUIPlugin implements IStartup {

	private static final String ECF_DISCOVERY_JMDNS = "ecf.discovery.jmdns"; //$NON-NLS-1$

	private static final String ECF_SINGLETON_DISCOVERY = "ecf.singleton.discovery"; //$NON-NLS-1$

	private static final String HUDSON_URL_PROPERTY_ID = "url"; //$NON-NLS-1$

	private static final String HUDSON_MDNS_ID = "hudson"; //$NON-NLS-1$

	// The plug-in ID
	public static final String PLUGIN_ID = "no.resheim.buildmonitor.hudson"; //$NON-NLS-1$

	// The shared instance
	private static HudsonPlugin plugin;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
	 * )
	 */
	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
	 * )
	 */
	@Override
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static HudsonPlugin getDefault() {
		return plugin;
	}

	@Override
	public void earlyStartup() {
		try {
			final IContainer container = getContainer();
			final IDiscoveryLocator adapter = (IDiscoveryLocator) getContainer()
					.getAdapter(IDiscoveryLocator.class);
			adapter.addServiceListener(new IServiceListener() {

				@Override
				public void serviceUndiscovered(IServiceEvent anEvent) {
					IServiceInfo info = anEvent.getServiceInfo();
					System.out.println(info);
					if (info.getServiceName().equals(HUDSON_MDNS_ID)) {
						IServiceProperties properties = info
								.getServiceProperties();
						try {
							URI uri = new URI(properties.getProperty(HUDSON_URL_PROPERTY_ID)
									.toString());
							if (BuildmonitorCore.getDefault().getServers()
									.hasServer(uri)) {
								Server server = BuildmonitorCore.getDefault()
										.getServers().getServer(uri);
								BuildmonitorCore
										.getDefault()
										.getServers()
										.removeServer(
												uri,
												MessageFormat
														.format(Messages.HudsonPlugin_Server_Removed,
																server));
							}
						} catch (URISyntaxException e) {
							e.printStackTrace();
						}
					}
				}

				@Override
				public void serviceDiscovered(IServiceEvent anEvent) {
					IServiceInfo info = anEvent.getServiceInfo();
					if (info.getServiceName().equals(HUDSON_MDNS_ID)) {
						IServiceProperties properties = info
								.getServiceProperties();
						try {
							URI uri = new URI(properties.getProperty(HUDSON_URL_PROPERTY_ID)
									.toString());
							if (!BuildmonitorCore.getDefault().getServers()
									.hasServer(uri)) {
								HudsonServerFactory f = new HudsonServerFactory();
								Server server = f.createInstance(uri);
								BuildmonitorCore
										.getDefault()
										.getServers()
										.addServer(
												server,
												MessageFormat
														.format(Messages.HudsonPlugin_Server_Added,
																server));
								server.connect();
							}
						} catch (URISyntaxException e) {
							e.printStackTrace();
						}
					}
				}
			});
			container.connect(null, null);

		} catch (ContainerCreateException e) {
			e.printStackTrace();
		} catch (ContainerConnectException e) {
			e.printStackTrace();
		}
	}

	protected IContainer getContainer() throws ContainerCreateException {
		return ContainerFactory.getDefault().createContainer(
				ECF_SINGLETON_DISCOVERY,
				new Object[] { ECF_DISCOVERY_JMDNS });
	}

}
